package com.bw.task.user.login;

import androidx.annotation.NonNull;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bw.library.mvp.view.BaseActivity;
import com.bw.task.MainActivity;
import com.bw.task.R;
import com.bw.task.bean.ImgBean;
import com.bw.task.bean.MoneyBean;
import com.bw.task.bean.PopupWindioBean;
import com.bw.task.bean.UserBean;
import com.bw.task.db.DaoMaster;
import com.bw.task.db.DaoSession;
import com.bw.task.db.User;
import com.bw.task.db.UserDao;
import com.bw.task.mvp.contern.UserContent;
import com.bw.task.mvp.model.UserModel;
import com.bw.task.mvp.persenter.UserPersenter;
import com.bw.task.user.right.RegActivity;

import java.util.List;

public  class LoginActivity extends BaseActivity<UserPersenter> implements UserContent.mView {

    /**
     * xch
     * 123
     */
    private android.widget.EditText username;
    private android.widget.EditText userpass;
    SharedPreferences.Editor edit;
    private android.widget.CheckBox cb;
    private android.widget.TextView logText;
    UserDao userDao;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what==100){
                try {
                    Thread.sleep(1000);
                    ToastUtils.showShort("登录成功,进入主界面");
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };

    @Override
    public int bandLayout() {
        return R.layout.act_login;
    }

    @Override
    public void initData() {
        DaoMaster.DevOpenHelper usera = new DaoMaster.DevOpenHelper(this, "usera");
        SQLiteDatabase writableDatabase = usera.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(writableDatabase);
        DaoSession daoSession = daoMaster.newSession();

        userDao = daoSession.getUserDao();


        mPresenter= new UserPersenter(this,new UserModel());
        SharedPreferences user = getSharedPreferences("user", MODE_PRIVATE);
        edit = user.edit();
        String name = user.getString("name", "");
        String pass = user.getString("pass", "");
        boolean bool = user.getBoolean("bool", false);
        username.setText(name);
        if (bool){
            userpass.setText(pass);
        }

        logText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this, RegActivity.class));
            }
        });
    }
    //登录
    public void login(View view) {
        String name = username.getText().toString().trim();
        String pass = userpass.getText().toString().trim();
        if (cb.isChecked()){

            edit.putString("name",name);
            edit.putString("pass",pass);
            edit.putBoolean("bool",true);
            edit.putBoolean("isuser",true);
            edit.commit();
            ToastUtils.showShort("已存入SP");
        }else {
            edit.clear();
            edit.commit();
        }

        mPresenter.login(name,pass);
    }

    @Override
    public void initView() {
        username = (EditText) findViewById(R.id.username);
        userpass = (EditText) findViewById(R.id.userpass);
        cb = (CheckBox) findViewById(R.id.cb);
        logText = (TextView) findViewById(R.id.log_text);
    }

    @Override
    public void UserBean(UserBean userBean) {

    }

    @Override
    public void LogUserBean(UserBean userBean) {
        int code = userBean.getCode();
        String userName = userBean.getData().getUserName();
        String userPwd = userBean.getData().getUserPwd();
        UserBean.DataBean data = userBean.getData();
        Log.i("---------1", "LogUserBean: "+userName);
        if (code==0){
            List<User> users1 = userDao.queryRaw("where name=? and pass=?", userName,userPwd);
            if (users1.size()==0){
                User user = new User();
                user.setName(userName);
                user.setPass(userPwd);
                String money = data.getMoney()+"";
                float v = Float.parseFloat(money);
                Log.i("---------money-----",v+"");
                user.setMoney(v);
                user.setUserid(userBean.getData().getUserId());
                userDao.insert(user);
                edit.putString("userid",userBean.getData().getUserId()+"");
                edit.commit();
                Log.i("--------------","存入数据库");
                handler.sendEmptyMessage(100);
            }

            List<User> users = userDao.loadAll();
            for (int i = 0; i < users.size(); i++) {
                LogUtils.i("数据库查询----"+"ID:"+users.get(i).getId()+"--name:"+users.get(i).getName()+"--pass:"+users.get(i).getPass()+"--mID:"+users.get(i).getUserid());
            }
            LogUtils.i(userBean.getCode()+"登录成功");
        /*    edit.putString("nameby",userBean.getData().getUserId()+"");
            edit.commit();*/
            handler.sendEmptyMessage(100);
        }
    }

    @Override
    public void MoneyUserBean(MoneyBean userBean) {

    }

    @Override
    public void getImgBean(ImgBean imgBean) {

    }


}